//
//  AlarmTypeStatisticsVC.swift
//  SBSafetyApp
//
//  Created by Lina on 2023/4/27.
//
// // 【首页-报警通知】报警通知-报警类型统计

import Foundation
import UIKit

class AlarmTypeStatisticsVC: SubLevelViewController {
    let detailV = AlarmTypeStatisticsView()

    var tbAlarmType: String = ""
    var name: String = ""

    init(withAlarmType alarmType: String, name: String, deptId: Int64? = nil, deptName: String? = nil) {
        super.init(nibName: nil, bundle: nil)
        self.tbAlarmType = alarmType
        self.name = name
        self.deptId = deptId
        self.deptName = deptName
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        view.backgroundColor = .bg
        self.title = name
        
        if deptName == nil {
            deptName = BSUser.currentUser.deptName
            deptId = BSUser.currentUser.deptId
        }
        
        setupUI()
        
        reloadData()
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        navigationController?.navBarStyle(.white)
    }
    
    func reloadData() {
        view.showToastActivity()
        // 预警类型统计
        API.getAlarmStaticsByType(withParam: AlarmStaticsParam(tbCompanyId: "\(deptId ?? -1)", tbAlarmType: self.tbAlarmType)) { responseModel in
            DispatchQueue.main.async {
                self.detailV.reloadData(withModal: responseModel.model)
                self.view.hideToastActivity()
            }
        }
    }
    
    // MARK: - Setup
    
    func setupUI() {
        view.addSubview(detailV)
        detailV.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview()
            make.width.equalTo(ScreenWidth)
            make.top.equalTo(view.safeAreaLayoutGuide.snp.top)
            make.bottom.equalTo(view.safeAreaLayoutGuide.snp.bottom)
        }
    }
}
